<?php 

namespace Cli\Controller;
use Think\Log;
use Api\Event\CacheEvent;

// +----------------------------------------------------------------------
// | 文件 TaskController.class.php
// +----------------------------------------------------------------------
// | 说明 常驻任务处理
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class TestController 
{

    public function push() {
        // $client = new \swoole_client(SWOOLE_SOCK_TCP);
        // if( !$client->connect("127.0.0.1", 6060 , 1) ) {
            // echo "Connect Error";
        // }
        $data = ['action' => 'push', 'data' => [
            'test'      => 1,
            'plat'      => 'ios',
            'type'      => 'sixin',
            'accounts'  => ['uestc110'],
            'custom'    => [
                'activity' => 'com.tv66.tv.ac.MyMessageConversationActivity',
                'content'  => "说一不二对你说: 哈哈哈",
                'nick'     => '说一不二'
            ],
        ]];
        local_tcp_request(6060, $data);
        // $json_data = json_encode($data);
        // $client->send( $json_data );
        exit;
    }

    public function trans_status($id = 0)
    {
        $Ucloud = new \Think\Upload\Driver\Ucloud(C('VIDEO_UPLOAD.rootPath'), C('UPLOAD_UCLOUD_VIDEO_CONFIG'));
        $status = $Ucloud->taskStatus($id);
        print_r($status);
    }

    public function trans_water()
    {
        $Ucloud = new \Think\Upload\Driver\Ucloud(C('VIDEO_UPLOAD.rootPath'), C('UPLOAD_UCLOUD_VIDEO_CONFIG'));
        print_r($Ucloud->watermarks());
    }

    public function upload_rank($start = 0, $end = 10000)
    {
        $Redis = CacheEvent::instance(0);
        load_config($Redis);
        unset($Redis);
        $start = intval($start);
        $end = intval($end);
        $games = service('Game')->where(['status' => 1])->getField('id',true);
        $key_all = 'IMBA.RANK.USER.UPLOAD.ALL';
        $key_game = 'IMBA.RANK.USER.UPLOAD.GAME.';
        if (!$start) {
            $Redis = CacheEvent::instance(7);
            $pipe = $Redis->multi(\Redis::PIPELINE);
            $pipe->delete($key_all);
            foreach ($games as $game_id) {
                $pipe->delete($key_game.$game_id);
            }
            $res = $pipe->exec();
            unset($Redis);
        }
        $exclude_uids = array_map('intval', C('EXCLUDE_USER_IDS', null, []));
        $match = [
            'status' => ['$gt' => -1],
            'race_id' => ['$eq' => null],
            'id' => ['$gte' => $start, '$lte' => $end],
            'uid' => ['$nin' => $exclude_uids],
        ];
        $Redis = CacheEvent::instance(7);
        $pipe = $Redis->multi(\Redis::PIPELINE);
        foreach ($games as $game_id) {
            $match['game_id'] = $game_id;
            $list = service('Video')->total($match, 'uid', 1);
            foreach ($list as $value) {
                $pipe->zIncrBy($key_all, $value['count'], $value['uid']);
                $pipe->zIncrBy($key_game.$game_id, $value['count'], $value['uid']);
            }
        }
        $res = $pipe->exec();
        unset($Redis);
    }
}